package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import com.shanxia.codec.enums.WipeOrderEnum;
import lombok.Data;

import static com.shanxia.codec.Decoder.getInt32;
import static com.shanxia.codec.Decoder.intToBytesLittle;

/**
 * S -> C
 * 服务器下发擦除指令
 */
@Data
public class Cmd1001 extends ProtocolDataBase {

    public static final int MIN_SIZE = 4;

    private WipeOrderEnum order;            // 擦除/查询指令

    public Cmd1001(){
        super.setCmd(1001);
    }

    public static Cmd1001 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd1001 cmd = new Cmd1001();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        //数据域长度
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        cmd.order = WipeOrderEnum.getByPos(getInt32(dataField, 0));
        return cmd;
    }

    @Override
    public byte[] getDataField() {
        byte[] data = new byte[MIN_SIZE];
        intToBytesLittle(data, 0, this.order.code);
        return data;
    }
}
